package splug.cjk;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Assign extends Thread{

	private String id;
	private String pw;
	private String name;
	private String car;
	private String phone;
	private String mail;
	private OutputStreamWriter osw;


	Assign(String id, String pw, String name, String car, String phone, String mail, OutputStreamWriter osw){
		this.id = id;
		this.pw = pw;
		this.name = name;
		this.car = car;
		this.phone = phone;
		this.mail = mail;
		this.osw = osw;
	}

	public void run(){

		try {

			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/carpool?useUnicode=true&characterEncoding=euckr","root","apmsetup");
			Statement stmt = conn.createStatement();

			String query = "INSERT INTO `carpool`.`member` (`id` ,`passwd` ,`name` ,`car` ,`phone` ,`mail` ) " +
					"VALUES ('" + id + "', '"+ pw + "', '" + name + "', '" + car + "', '" + phone + "', '" + mail + "');";

			System.out.println(query);

			stmt.execute("SET CHARACTER SET euckr");
			stmt.execute("set names euckr");
			stmt.executeUpdate(query);

			osw.write('1');
			osw.flush();

			stmt.close();
			conn.close();

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			
			try {
				osw.write('0');
				osw.flush();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
